/*
 * This class is part of the "gwt-tinymce" library.  
 * Copyright (C)2008 Collegeman.net, LLC.  
 * Adjustments for the Netzpult project Copyright (c) 2011 Ulrich Hilger, uhilger.de
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package de.uhilger.netzpult.client;

import com.google.gwt.user.client.ui.Composite;

/**
 * A GWT-Composite for TinyMCE
 * 
 * <p>Adjusted from http://code.google.com/p/gwt-tinymce/</a>
 * 
 * @author by Aaron Collegeman, http://collegeman.net 
 * @author adjustments for the Netzpult project by Ulrich Hilger, http://uhilger.de
 * @author Published under the terms and conditions of
 * the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
 */
public class Editor extends Composite {

	private boolean active = false;
	protected String id;

	public final void toggleEditor() {
		execCommand(!active ? "mceAddControl" : "mceRemoveControl", false, id);
		active = !active;
	}

	public String getId() {
		return id;
	}

	public String getText() {
		return getEditorText(id);
	}

	public boolean setText(String text) {
		try {
			setEditorText(id, text);
			return true;
		} catch(Exception e) {
			//Window.alert("Editor.setText" + e.getMessage());
			return false;
		}
	}

	public final native void execCommand(String c, boolean b, String v) /*-{
		$wnd.tinyMCE.execCommand(c, b, v);
	}-*/;

	protected native void setEditorText(String editorId, String text) /*-{
		var ed = $wnd.tinyMCE.get(editorId);
		ed.setContent(text);
	}-*/;

	protected native String getEditorText(String editorId) /*-{
		var ed = $wnd.tinyMCE.get(editorId);
		return ed.getContent();
	}-*/;

}
